package com.wxn.learncompose.pagers

import android.util.Log
import androidx.compose.animation.core.Animatable
import androidx.compose.animation.core.VectorConverter
import androidx.compose.foundation.background
import androidx.compose.foundation.gestures.awaitFirstDown
import androidx.compose.foundation.layout.Box
import androidx.compose.foundation.layout.Spacer
import androidx.compose.foundation.layout.fillMaxSize
import androidx.compose.foundation.layout.offset
import androidx.compose.foundation.layout.size
import androidx.compose.foundation.shape.RoundedCornerShape
import androidx.compose.runtime.Composable
import androidx.compose.runtime.remember
import androidx.compose.runtime.rememberCoroutineScope
import androidx.compose.ui.Modifier
import androidx.compose.ui.geometry.Offset
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.graphics.graphicsLayer
import androidx.compose.ui.input.pointer.pointerInput
import androidx.compose.ui.unit.Density
import androidx.compose.ui.unit.IntOffset
import androidx.compose.ui.unit.dp
import kotlinx.coroutines.coroutineScope
import kotlinx.coroutines.launch
import kotlin.math.roundToInt

@Composable
fun GustureAnimPager() {
//    val scope = rememberCoroutineScope()
//
//    val offset  = remember{ Animatable(Offset(0f, 0f), Offset.VectorConverter) }
//
//    Box(modifier = Modifier
//        .fillMaxSize()
//        .pointerInput(Unit) {
//            coroutineScope {
//                while(true) {
//                    val position = awaitPointerEventScope {
//                        val p = awaitFirstDown().position
//                        Log.d("GustureAnimPager", "actionDown : position = ${p.x}, ${p.y}")
//                        p
//                    }
//                    launch {
//                        offset.animateTo(position)
//                    }
//                }
//            }
//        }){
//
//        Spacer(modifier = Modifier
//            .background(Color.Blue, RoundedCornerShape(4.dp))
//            .size(46.dp)
//            .offset { offset.value.toIntOffset() })
//
//    }
    val offset = remember { Animatable(Offset(0f, 0f), Offset.VectorConverter) }
    Box(
        modifier = Modifier
            .fillMaxSize()
            .pointerInput(Unit) {
                coroutineScope {
                    while (true) {
                        // 检测一个点击头事件并获得其位置。
                        val position = awaitPointerEventScope {
                            awaitFirstDown().position
                        }
                        launch {
                            // 应用到点击的位置。
                            offset.animateTo(position)
                        }
                    }
                }
            }
    ) {
        Spacer(modifier = Modifier.size(32.dp).background(Color.Blue)
            .graphicsLayer {
                translationX = offset.value.x
                translationY = offset.value.y
            }
//            .offset { offset.value.toIntOffset() }
        )
    }
}

private fun Offset.toIntOffset() = IntOffset(x.roundToInt(), y.roundToInt())
